<?php

/**
 * page actions.
 *
 * @package    khas
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 3335 2007-01-23 16:19:56Z fabien $
 */
class contentActions extends sfActions
{
    public function preExecute()
    {
        ProjectConfiguration::getApplicationConfiguration('front', 'prod', false)->loadHelpers('Url');
    }
    
    public function executeShow(sfWebRequest $request)
    {
        $this->news = $news = Doctrine::getTable('News')->find($request->getParameter('id'));
        $this->forward404Unless($news);
        
        $breadcrumbs = array();
        $breadcrumbs[] = '<span>&raquo;</span><a href="'.url_for('content/index').'">Мэдээ мэдээлэл</a>';
        $breadcrumbs[] = '<span>&raquo;</span><a href="'.url_for('content/show?id='.$news->getId()).'">'.$news.'</a>';
        $this->getResponse()->setSlot('breadcrumbs', $breadcrumbs);
    }
    
    
    public function executeIndex(sfWebRequest $request)
    {
        $params = array();
        $this->pager = Doctrine::getTable('News')->getPager($params, $request->getParameter('page'));
        
        $breadcrumbs = array();
        $breadcrumbs[] = '<span>&raquo;</span><a href="'.url_for('content/index').'">Мэдээ мэдээлэл</a>';
        $this->getResponse()->setSlot('breadcrumbs', $breadcrumbs);
        
    }
    
    
    
    public function executeVideoShow(sfWebRequest $request)
    {
        $this->video = $video = Doctrine::getTable('Video')->find($request->getParameter('id'));
        $this->forward404Unless($video);
    }
    
    
    public function executeAlbumShow(sfWebRequest $request)
    {
        $this->album = $album = Doctrine::getTable('Album')->find($request->getParameter('id'));
        $this->forward404Unless($album);
    }
    
    
    
    
    public function executeVote(sfWebRequest $request)
    {
        $this->message = '';
    
        if($request->isMethod(sfRequest::POST))
        {
          if($poll = Doctrine_Core::getTable('Poll')->find($request->getParameter('poll_id')))      
          {
            $options = $request->getParameter('options');
            if($option = Doctrine_Core::getTable('PollOption')->find($options[0]))
            {        
                $option->setNumber($option->getNumber()+1);
                $option->save();

                // save in session
                $votedIds = $this->getUser()->getAttribute('voteIds', array());
                $votedIds[] = $poll->getId();
                $this->getUser()->setAttribute('voteIds', $votedIds);

                $this->message = 'Амжилттай саналаа өглөө.';
             }
             else $this->message = 'Та санал өгөх сонголтоо хийнэ үү.';
              
             $this->poll = $poll;
             $this->setLayout(false);
             return sfView::SUCCESS;
          }
        }
        return sfView::NONE;
    }


}